package net.myspring.middle.service;

import com.google.common.collect.Lists;
import net.myspring.common.mybatis.domain.Page;
import net.myspring.common.mybatis.domain.Pageable;
import net.myspring.common.utils.Collections3;
import net.myspring.common.utils.Const;
import net.myspring.common.utils.StringUtils;
import net.myspring.middle.domain.Department;
import net.myspring.middle.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2016/8/11.
 */
@Service
public class DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    public Department findOne(String id) {
        return departmentMapper.findOne(id);
    }

    @Transactional
    public Department delete(Department department) {
        department.setEnabled(false);
        departmentMapper.update(department);
        return department;
    }

    public Page<Department> findPage(Pageable pageable, Map<String, Object> map) {
        Page<Department> page = departmentMapper.findPage(pageable, map);
        return page;
    }

    public List<Department> findAll() {
        List<Department> departments = Lists.newArrayList();
        List<Department> sourceList = departmentMapper.findAll();
        sortList(departments, sourceList, Const.ROOT_ID);
        for (Department department : departments) {
            String[] space = new String[department.getParentIds().split(Const.CharEnum.COMMA.getValue()).length - Const.ROOT_PARENT_IDS.split(Const.CharEnum.COMMA.getValue()).length];
            Arrays.fill(space, Const.CharEnum.SPACE_FULL.getValue());
            department.setFullName(StringUtils.join(space) + department.getName().trim());
        }
        return departments;
    }

    @Transactional
    public Department save(Department department) {
        department.setParentIds(getParentIds(department));
        if (department.isCreate()) {
            departmentMapper.save(department);
        } else {
            departmentMapper.update(department);
        }
        return department;
    }

    public boolean checkName(Department department) {
        Department checkDepartment = departmentMapper.findByName(department.getName());
        return checkDepartment == null || checkDepartment.getId().equals(department.getId());
    }

    private Boolean getDeletable(Department department) {
        List<Department> departments = departmentMapper.findByParentId(department.getId());
        if (Collections3.isEmpty(departments)) {
            return true;
        } else {
            return false;
        }
    }

    private void sortList(List<Department> list, List<Department> sourceList, String parentId) {
        for (int i = 0; i < sourceList.size(); i++) {
            Department e = sourceList.get(i);
            if (e.getParentId().equals(parentId)) {
                list.add(e);
                // 判断是否还有子节点, 有则继续获取子节点
                for (int j = 0; j < sourceList.size(); j++) {
                    Department child = sourceList.get(j);
                    if (child.getParentId().equals(e.getId())) {
                        sortList(list, sourceList, e.getId());
                        break;
                    }
                }
            }
        }
    }

    private String getParentIds(Department department) {
        String parentIds = "";
        Department parent = departmentMapper.findOne(department.getParentId());
        parentIds = parent.getParentIds() + Const.CharEnum.COMMA.getValue() + parent.getId();
        return parentIds;
    }
}
